enum ConsoleMessageType {
    log = "log",
    info = "info",
    debug = "debug",
    warn = "warn",
    error = "error",
}
interface ConsoleMessage {
    message: any;
    type: ConsoleMessageType;
}
export class MessageWriter {
    static _instance: MessageWriter;
    private messageList: ConsoleMessage[] = [];
    private consoleMessageContainer: HTMLElement | null = null;
    private clearConsole: boolean = false;
    constructor(method?: Function | null, container?: HTMLElement, clearConsole: boolean = false) {
        if (MessageWriter._instance) {
            this.clearMessageList();
            method && method();
            return MessageWriter._instance;
        }
        container && (this.consoleMessageContainer = container);
        this.clearConsole = clearConsole;
        MessageWriter._instance = this;
        method && method();
    }
    addMessage = (message: ConsoleMessage) => {
        this.messageList.push(message);
    };
    writeMessage = () => {
        const documentFragment = new DocumentFragment();
        this.consoleMessageContainer!.innerHTML = "";
        this.messageList.forEach(({ message, type }, index) => {
            const div = document.createElement("div");
            div.innerHTML = `[${type}*${index}]:` + JSON.stringify(Object.values(message));
            div.className = type;
            documentFragment.appendChild(div);
        });
        this.consoleMessageContainer!.appendChild(documentFragment);
        this.clearMessageList();
        this.clearConsole && console.clear();
    };
    clearMessageList = () => {
        this.messageList = [];
    };
}
export function consoleInterceptor(messageWriter: MessageWriter) {
    const $console: { [key in ConsoleMessageType]: Function } = {
        log: console.log,
        info: console.info,
        debug: console.debug,
        warn: console.warn,
        error: console.error,
    };
    for (const key in $console) {
        if (Object.prototype.hasOwnProperty.call($console, key)) {
            //@ts-ignore
            console[key] = function () {
                //@ts-ignore
                $console[key].apply(this, [...arguments]);
                messageWriter.addMessage({
                    message: arguments,
                    //@ts-ignore
                    type: key,
                });
            };
        }
    }
}
